package com.example.algorithm.model;

import org.springframework.util.ObjectUtils;

/**
 * @auth Administrator
 * @create @2024/7/24@20:35
 * @decription
 **/
public class SingleNodeList {

    private Node head;

    public Node getHead() {
        return head;
    }

    /**
     * 尾插法
     */
    public void addNodeAtTail(int [] arr){ // 添加链表节点
        Node  newNode = new Node(arr[0]);
        if(arr.length==2){
            Node random = new Node(arr[1]);
            newNode.random = random;
        }
        if(head==null){
            head = newNode;
        }else{
            Node temp = head;
            while (temp.next != null){ // 当前节点的next为null的时候遍历结束
                temp = temp.next;
            }
            temp.next = newNode;
        }
    }

    public void print(Node node){
        Node curr = node;
        System.out.print("链表：[");
        while (curr!=null){
            System.out.print("["+curr.val+","+curr.random+"]");
            if(curr.next!=null){
                System.out.print(",");
            }
            curr = curr.next;

        }
        System.out.println("]");

    }
}
